﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WL_WinForm
{
    public partial class FrmNote : DockContent
    {
        public FrmNote()
        {
            InitializeComponent();
            BindData();
        }

        private void BindData()
        {
            DataTable dt = new WLong.BLL.CustomerMessage().GetList("Disabled=0").Tables[0];
            gvNote.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            WLong.BLL.CustomerMessage note = new WLong.BLL.CustomerMessage();
            if (txtCustomerMessageID.Text !="")
            {
                WLong.Model.CustomerMessage mNote = new WLong.Model.CustomerMessage();
                mNote.CustomerMessageID = new Guid(txtCustomerMessageID.Text);
                mNote.CustomerName = txtCustomerName.Text.Trim();
                mNote.CustomerTelephone = txtCustomerTelephone.Text.Trim();
                mNote.CustomerMobile = txtCustomerMobile.Text.Trim();
                mNote.CustomerEmail = txtCustomerEmail.Text.Trim();
                mNote.CustomerContent = txtCustomerContent.Text.Trim();
                mNote.Disabled = 0;
                mNote.REC_ModifyBy = COMMON.Login.loginName;
                mNote.REC_ModifyTime = DateTime.Now;

                if (note.Update(mNote))
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("修改失败！");
                }
            }
            else
            {
                WLong.Model.CustomerMessage mNote = new WLong.Model.CustomerMessage();
                mNote.CustomerMessageID = Guid.NewGuid();
                mNote.CustomerName = txtCustomerName.Text.Trim();
                mNote.CustomerTelephone = txtCustomerTelephone.Text.Trim();
                mNote.CustomerMobile = txtCustomerMobile.Text.Trim();
                mNote.CustomerEmail = txtCustomerEmail.Text.Trim();
                mNote.CustomerContent = txtCustomerContent.Text.Trim();
                mNote.Disabled = 0;
                mNote.REC_CreateBy = COMMON.Login.loginName;
                mNote.REC_CreateTime = DateTime.Now;

                note.Add(mNote);
                ClearControl();
                BindData();
            }
        }

        private void ClearControl()
        {
            txtCustomerContent.Text = "";
            txtCustomerEmail.Text = "";
            txtCustomerMessageID.Text = "";
            txtCustomerMobile.Text = "";
            txtCustomerName.Text = "";
            txtCustomerTelephone.Text = "";
        }

        private void gvNote_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCustomerContent.Text = gvNote.CurrentRow.Cells["CustomerContent"].Value.ToString();
            txtCustomerEmail.Text = gvNote.CurrentRow.Cells["CustomerEmail"].Value.ToString();
            txtCustomerMessageID.Text = gvNote.CurrentRow.Cells["CustomerMessageID"].Value.ToString();
            txtCustomerMobile.Text = gvNote.CurrentRow.Cells["CustomerMobile"].Value.ToString();
            txtCustomerName.Text = gvNote.CurrentRow.Cells["CustomerName"].Value.ToString();
            txtCustomerTelephone.Text = gvNote.CurrentRow.Cells["CustomerTelephone"].Value.ToString();
        }

        private void gvNote_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                WLong.BLL.CustomerMessage note = new WLong.BLL.CustomerMessage();
                WLong.Model.CustomerMessage mNote = new WLong.Model.CustomerMessage();
                mNote.CustomerMessageID = new Guid(gvNote.CurrentRow.Cells["CustomerMessageID"].Value.ToString());
                mNote.CustomerName = gvNote.CurrentRow.Cells["CustomerName"].Value.ToString();
                mNote.CustomerTelephone = gvNote.CurrentRow.Cells["CustomerTelephone"].Value.ToString();
                mNote.CustomerMobile = gvNote.CurrentRow.Cells["CustomerMobile"].Value.ToString();
                mNote.CustomerEmail = gvNote.CurrentRow.Cells["CustomerEmail"].Value.ToString();
                mNote.CustomerContent = gvNote.CurrentRow.Cells["CustomerContent"].Value.ToString();
                mNote.Disabled = 1;
                mNote.REC_ModifyBy = COMMON.Login.loginName;
                mNote.REC_ModifyTime = DateTime.Now;

                if (note.Update(mNote))
                {
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }
            }
        }
    }
}
